<template>
  <el-dialog title="分配权限" :visible.sync="show" @close="closeEvent">
    <!-- defaultKeys:不考虑以前选中项.只考虑现在的值要选中(它的选中会累加) -->
    <el-tree ref="tree" node-key="id" :default-checked-keys="defaultKeys" show-checkbox check-strictly :data="list" :props="{label:'name'}" />
    <template #footer>
      <div style="text-align:center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="show=false">取消</el-button>
      </div>
    </template>

  </el-dialog>
</template>

<script>
import { sysPermission } from '@/api/permission'
import { changeData } from '@/utils/index'
import { sysRoleById, sysRoleAssignPrem } from '@/api/setting'
export default {
  data() {
    return {
      show: false,
      list: [],
      defaultKeys: [],
      id: ''
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async  getData() {
      const res = await sysPermission()
      this.list = changeData(res.data, '0')
      console.log('权限列表数据', res)
    },
    // 获取角色详情
    async   getInfo(id) {
      this.id = id
      const res = await sysRoleById(id)
      // setCheckedKeys:需要el-tree已渲染完成才可用
      // this.$refs.tree.setCheckedKeys(res.data.permIds)
      // default-checked-keys:不用考虑有没有渲染完成
      this.defaultKeys = res.data.permIds
      console.log('角色详情', res)
    },
    closeEvent() {
      // this.defaultKeys = []
      // 清空选中
      this.$refs.tree.setCheckedKeys([])
    },
    // 确定点击事件
    async submit() {
      await sysRoleAssignPrem({
        id: this.id,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.show = false
      this.$message.success('分配权限成功')
    }
  }

}
</script>

<style>

</style>
